﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SQLite;

namespace SEO.DBSqlHelp
{
    /// <summary>
    /// 版权所有: 版权所有(C) 2011，云里网络科技
    /// 内容摘要: DBHelper
    /// 完成日期：2011年9月15日
    /// 版    本：V1.0 
    /// 作    者：Adin
    /// </summary>
    public class ESDBHelper
    {
        private static SQLiteConnection cn;
        private static string strConn = "Data Source=" + System.Windows.Forms.Application.StartupPath + "\\data\\emails.db";

        /// <summary>
        /// 连接电子邮件采集数据库
        /// </summary>
        /// <returns>返回电子邮件采集数据库连接字符串</returns>
        public static string ConnectionEmailSearch()
        {
            return "Data Source=" + System.Windows.Forms.Application.StartupPath + "\\data\\emails.db";
        }

        /// <summary>
        /// 连接数据库
        /// </summary>
        /// <returns>返回SQLiteConnection实例</returns>
        public static SQLiteConnection GetCon()
        {
            if (cn == null)
            {
                cn = new SQLiteConnection(strConn);
            }
            return cn;
        }

        /// <summary>
        /// 执行数据库操作语句
        /// </summary>
        /// <param name="strSql">SQL语句</param>
        /// <returns>返回影响行数</returns>
        public static int ExecuteSql(string strSql)
        {
            int iNum = -1;
            SQLiteConnection cn = ESDBHelper.GetCon();
            SQLiteCommand cm = new SQLiteCommand(strSql, cn);
            try
            {
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                iNum = cm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("执行数据库操作语句 -> " + ex.Message);
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                {
                    cn.Close();
                }
            }
            return iNum;
        }

        /// <summary>
        /// 函数输入SQL语句，输出一个结果集（DATATABLE）
        /// </summary>
        /// <param name="strSql">SQL语句</param>
        /// <returns>返回 DataTable</returns>
        public static DataTable GetTable(string strSql)
        {
            DataTable dt = new DataTable();
            SQLiteConnection cn = ESDBHelper.GetCon();
            try
            {
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataAdapter da = new SQLiteDataAdapter(cm);
                da.Fill(dt);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("函数输入SQL语句，输出一个结果集（DATATABLE） -> " + ex.Message);
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                {
                    cn.Close();
                }
            }
            return dt;
        }

        /// <summary>
        /// 函数输入SQL语句，返回一个SQLiteDataReader结果集
        /// </summary>
        /// <param name="strSql">SQL语句</param>
        /// <returns>返回 SQLiteDataReader 实例</returns>
        public static SQLiteDataReader ExReader(string strSql)
        {
            SQLiteConnection cn = ESDBHelper.GetCon();
            SQLiteDataReader dr = null;
            try
            {
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                dr = cm.ExecuteReader(CommandBehavior.CloseConnection);

            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("函数输入SQL语句，返回一个SQLiteDataReader结果集 -> " + ex.Message);
            }

            return dr;
        }
    }
}
